package com.app.fenak;

import java.io.FileNotFoundException;
import java.io.InputStream;

import com.app.fenak.asynctasks.RegisterAsyncTask;
import com.app.fenak.dto.User;
import com.app.fenak.validation.ExpressionValidation;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class RegisterActivity extends Activity {

	private EditText firstName, lastName, userName, email, password,
			confirmPassword;
	private ImageView userImage;
	private TextView mismatchError, usernameError;
	private int PIC_SELECTED = 1;
	private String first_name, last_name, username, email_string, pass,
			confirm_pass;
	private Bitmap user_Image;
	private Button register;
	private ProgressDialog progressDialog;
	private SharedPreferences sharedPreferences;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);

		firstName = (EditText) findViewById(R.id.first_name);
		lastName = (EditText) findViewById(R.id.last_name);
		userName = (EditText) findViewById(R.id.username);
		email = (EditText) findViewById(R.id.email);
		password = (EditText) findViewById(R.id.password);
		confirmPassword = (EditText) findViewById(R.id.confirm_password);

		userImage = (ImageView) findViewById(R.id.user_image);

		mismatchError = (TextView) findViewById(R.id.password_error);
		usernameError = (TextView) findViewById(R.id.username_error);

		register = (Button) findViewById(R.id.register_button);
		sharedPreferences = getSharedPreferences(getString(R.string.pref_name),
				Context.MODE_PRIVATE);

	}

	@Override
	protected void onStart() {

		super.onStart();

		email.setText(sharedPreferences.getString(
				getString(R.string.email_pref), ""));

		userImage.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent intent = new Intent();
				intent.setType("image/*");
				intent.setAction(Intent.ACTION_GET_CONTENT);
				startActivityForResult(
						Intent.createChooser(intent, "Select Picture"),
						PIC_SELECTED);
			}
		});

		register.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				boolean isValid = formValidation();

				if (isValid) {

					User user = new User();
					user.setEmail(email_string);
					user.setFirstName(first_name);
					user.setLastName(last_name);
					user.setPassword(pass);
					user.setUserName(username);
					user.setUserImage(user_Image);

					progressDialog = ProgressDialog.show(RegisterActivity.this,
							"", getString(R.string.register_dialog));
					new RegisterAsyncTask()
							.execute(RegisterActivity.this, user);

				}

			}
		});

	}

	private boolean formValidation() {

		first_name = firstName.getText().toString();
		last_name = lastName.getText().toString();
		username = userName.getText().toString();
		email_string = email.getText().toString();
		pass = password.getText().toString();
		confirm_pass = confirmPassword.getText().toString();

		if (username.equalsIgnoreCase("")) {
			usernameError.setVisibility(View.VISIBLE);
			return false;
		} else {
			usernameError.setVisibility(View.GONE);
		}
		if (pass.equalsIgnoreCase("")
				|| ExpressionValidation.IsMatch(pass,
						ExpressionValidation.PASSWORD_PATTERN)) {

			mismatchError.setText(getString(R.string.valid_password));
			mismatchError.setVisibility(View.VISIBLE);
			return false;
		} else {
			mismatchError.setVisibility(View.GONE);
		}
		if (!pass.equalsIgnoreCase(confirm_pass)) {
			mismatchError.setText(getString(R.string.password_mismatch));
			mismatchError.setVisibility(View.VISIBLE);
			return false;
		} else {
			mismatchError.setVisibility(View.GONE);
		}

		return true;

	}

	public void dismissProgressDialog() {
		if (this.progressDialog != null) {
			this.progressDialog.dismiss();
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (resultCode == RESULT_OK) {
			if (requestCode == PIC_SELECTED) {
				Uri selectedImageUri = data.getData();
				InputStream imageStream = null;
				try {
					imageStream = getContentResolver().openInputStream(
							selectedImageUri);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				user_Image = BitmapFactory.decodeStream(imageStream);
				userImage.setImageBitmap(user_Image);
				// here i'm supposed to send this image to the server.
				Log.i("pic uri", selectedImageUri.toString());
				// selectedImagePath = getPath(selectedImageUri);
			}
		}
	}

}

/**
 * this part to test how to get a photo from the phone gallery
 */
// Intent i = new Intent(
// Intent.ACTION_PICK,
// android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// startActivity(i);

